package com.bicon.botu.mall.component.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bicon.botu.mall.component.common.BannerConstants;
import com.bicon.botu.mall.component.processor.rpc.service.GoodsInvoker;
import com.bicon.botu.mall.component.service.IBannerService;
import com.bicon.botu.mall.component.entity.Banner;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.core.base.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图 前端控制器
 *
 * @author 张凡
 * @date 2017/7/19 下午 14:49
 */
@RestController
@CrossOrigin
@RequestMapping("/V1.0.0/banner")
public class BannerController extends BaseController {
    @Autowired
    IBannerService iBannerService;

    @Autowired
    GoodsInvoker goodsInvoker;

    /**
     * 增加轮播图
     *
     * @param bannersJson 轮播图对象集合
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam(value = "bannerModular") Integer bannerModular,
                             @RequestParam(value = "bannerPosition") Integer bannerPosition,
                             @RequestParam(value = "bannersJson") String bannersJson) {
        //判断正常状态的轮播图有多少个，5个时，返回提示
        List<Banner> bannersList = new Banner().selectList(new EntityWrapper<Banner>()
                .eq("banner_position", bannerPosition)
                .eq("banner_modular", bannerModular)
                .eq("logic_delete", BannerConstants.DEL_LOGIC_NORMAL));
        Integer size = bannersList.size();
        if (size >= 5) {
            return new BaseResult(BannerConstants.RESPONSE_CODE_604, BannerConstants.RESPONSE_MESSAGE_604);
        }
        List<Banner> banners = JSONObject.parseArray(bannersJson, Banner.class);
        for (Banner banner : banners) {
            if (banner.getResponseType() == BannerConstants.URL) {
                //若为url，判断url是否为空
                if (StringUtils.isBlank(banner.getBannerUrl())) {
                    return new BaseResult(BannerConstants.RESPONSE_CODE_400, BannerConstants.RESPONSE_MESSAGE_400);
                }
            } else if (banner.getResponseType() == BannerConstants.GOODS) {
                //若为商品，判断商品id是否为空
                if (StringUtils.isBlank(banner.getGoodsIds())) {
                    return new BaseResult(BannerConstants.RESPONSE_CODE_400, BannerConstants.RESPONSE_MESSAGE_400);
                }
                //调rpc
                goodsInvoker.updateProductState(banner.getGoodsIds(),"1");
            }
            if (banner.getBannerPosition() == null || banner.getBannerModular() == null || banner.getBannerFlag() == null) {
                return new BaseResult(BannerConstants.RESPONSE_CODE_400, BannerConstants.RESPONSE_MESSAGE_400);
            }
            banner.preInsert();
        }

        Boolean flag = iBannerService.insert(bannerModular, bannerPosition, banners);
        if (flag) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, flag);
        } else {
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500, flag);
        }
    }

    /**
     * 逻辑删除轮播图
     *
     * @param idJson 轮播图Id集合
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public BaseResult delete(@RequestParam(value = "bannerModular") Integer bannerModular,
                             @RequestParam(value = "bannerPosition") Integer bannerPosition,
                             @RequestParam("idJson") String idJson) {
        return iBannerService.delete(bannerModular, bannerPosition, idJson);
    }

    /**
     * 修改轮播图
     *
     * @param bannersJson 轮播图对象集合
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam(value = "bannerModular") Integer bannerModular,
                             @RequestParam(value = "bannerPosition") Integer bannerPosition,
                             @RequestParam(value = "bannersJson") String bannersJson) {
        List<Banner> banners = JSONObject.parseArray(bannersJson, Banner.class);
        return iBannerService.update(bannerModular, bannerPosition, banners);
    }

    /**
     * 初始化轮播图到redis
     *
     * @param bannerModular  轮播图模块
     * @param bannerPosition 轮播图位置
     * @return
     */
    @RequestMapping(value = "/initBannerToRedis", method = RequestMethod.POST)
    public BaseResult initBannerToRedis(@RequestParam(value = "bannerModular") Integer bannerModular,
                                        @RequestParam(value = "bannerPosition") Integer bannerPosition) {
        return iBannerService.initBannerToRedis(bannerModular, bannerPosition);
    }

    /**
     * 获取轮播图
     *
     * @param bannerModular  轮播图模块
     * @param bannerPosition 轮播图位置
     * @return
     */
    @RequestMapping(value = "/getBannerDetail", method = RequestMethod.POST)
    public BaseResult getBannerDetail(@RequestParam(value = "bannerModular") Integer bannerModular,
                                      @RequestParam(value = "bannerPosition") Integer bannerPosition) {
        //查询出所有正常的轮播图
        List<Banner> banners = new Banner().selectList(new EntityWrapper<Banner>()
                .eq("banner_position", bannerPosition).eq("banner_modular", bannerModular)
                .eq("logic_delete", BannerConstants.DEL_LOGIC_NORMAL).orderBy("banner_sort", false));
        return new BaseResult(BannerConstants.RESPONSE_CODE_200, BannerConstants.RESPONSE_MESSAGE_200, banners);
    }

    /**
     * 修改排序
     *
     * @param bannerModular  轮播图模块
     * @param bannerPosition 轮播图位置
     * @param bannerId       轮播图id
     * @param operation      向上还是向下
     * @return
     */
    @RequestMapping(value = "/modifyBannerSort", method = RequestMethod.POST)
    public BaseResult bannerSort(@RequestParam(value = "bannerModular") Integer bannerModular,
                                 @RequestParam(value = "bannerPosition") Integer bannerPosition,
                                 @RequestParam(value = "bannerId") Integer bannerId,
                                 @RequestParam(value = "operation") String operation) {
        return iBannerService.bannerSort(bannerModular, bannerPosition, bannerId, operation);
    }
}
